package edu.isquen.core.jfreechart.service;

import java.awt.Color;
import java.awt.Font;
import java.io.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class JFreechartService {

	public static JFreeChart getCategoryChart() {
		CategoryDataset dataset = getCategoryData();
		JFreeChart chart = ChartFactory.createBarChart3D("FRUIT QUALITY", // 图表标题
				"FRUIT", // 目录轴的显示标签
				"QUALITY", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向：水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);
		setFont(chart);
		return chart;
	}

	public static JFreeChart getPieChart() {
		DefaultPieDataset piedataset = getPieData();
		JFreeChart pieChart = ChartFactory.createPieChart3D("FRUIT QUALITY", // 图表标题
				(PieDataset) piedataset, // 数据集
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);
		setFont(pieChart);
		return pieChart;
	}

	public static JFreeChart getLineChart() {
		CategoryDataset dataset = getCategoryData();
		JFreeChart chart = ChartFactory.createLineChart("FRUIT QUALITY",
				"FRUIT", "QUALITY", dataset, PlotOrientation.VERTICAL, true,
				false, false);
		setFont(chart);
		return chart;
	}

	/**
	 * 获取一个演示用的简单数据集对象
	 * 
	 * @return
	 */
	private static DefaultPieDataset getPieData() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("苹果", 100);
		dataset.setValue("梨子", 200);
		dataset.setValue("葡萄", 300);
		dataset.setValue("香蕉", 400);
		dataset.setValue("荔枝", 500);
		return dataset;
	}

	/**
	 * 获取一个演示用的组合数据集对象
	 * 
	 * @return
	 */
	private static CategoryDataset getCategoryData() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(300, "beijing", "苹果");
		dataset.addValue(100, "shanghai", "苹果");
		dataset.addValue(200, "guangzhou", "苹果");
		dataset.addValue(500, "beijing", "梨子");
		dataset.addValue(600, "shanghai", "梨子");
		dataset.addValue(400, "guangzhou", "梨子");
		dataset.addValue(700, "beijing", "葡萄");
		dataset.addValue(800, "shanghai", "葡萄");
		dataset.addValue(900, "guangzhou", "葡萄");
		dataset.addValue(1500, "beijing", "香蕉");
		dataset.addValue(1100, "shanghai", "香蕉");
		dataset.addValue(1000, "guangzhou", "香蕉");
		dataset.addValue(500, "beijing", "荔枝");
		dataset.addValue(1700, "shanghai", "荔枝");
		dataset.addValue(500, "guangzhou", "荔枝");
		return dataset;
	}

	public static void setFont(JFreeChart chart) {
		Font font = new Font("宋体", Font.ITALIC, 12);
		if (chart.getPlot() instanceof PiePlot) {
			PiePlot plot = (PiePlot) chart.getPlot();
			plot.setLabelFont(font);
		} else if (chart.getPlot() instanceof CategoryPlot) {
			CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
			plot.setNoDataMessageFont(font);
			plot.setDomainGridlinesVisible(true);
			plot.setRangeGridlinesVisible(true);
			plot.setDomainGridlinePaint(Color.white);
			plot.setRangeGridlinePaint(Color.white);
			//显示点数据
//			LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot.getRenderer();
//			lineandshaperenderer.setBaseShapesVisible(true); 
//			lineandshaperenderer.setBaseLinesVisible(true); 

		}
		chart.getTitle().setFont(font);
		chart.getLegend().setItemFont(font);
	}

	public static void main(String[] args) throws IOException {

		CategoryDataset dataset = getCategoryData();
		JFreeChart chart = ChartFactory.createBarChart("FRUIT QUALITY", // 图表标题
				"FRUIT", // 目录轴的显示标签
				"QUALITY", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向：水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);

		DefaultPieDataset piedataset = getPieData();
		JFreeChart pieChart = ChartFactory.createPieChart3D("FRUIT QUALITY", // 图表标题
				(PieDataset) piedataset, // 数据集
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);

		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("G:\\fruit.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 800, 400);
			fos_jpg = new FileOutputStream("G:\\fruitPie.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg, 1, pieChart, 800, 400);
		} finally {
			try {
				fos_jpg.close();
			} catch (Exception e) {
			}
		}
	}
}